package com.wx.mall.operate.controller;


import com.alibaba.fastjson.JSON;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.RebGoodsTwoDto;
import com.wx.mall.operate.dto.VGoodsDto;
import com.wx.mall.operate.entity.VGoods;
import com.wx.mall.operate.service.impl.VGoodsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/v/goods")
public class VGoodsController {

    @Autowired
    VGoodsServiceImpl vGoodsService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/getFindGoodsPost")
    public CommonResult<CommonPage<VGoods>> getFindGoods(VGoodsDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<VGoods> goods = vGoodsService.findGoodsAll(dto);
        return CommonResult.success(CommonPage.restPage(goods));
    }

    @ApiOperation(value = "筛选分页查询")
    @GetMapping("/getScreenVGoods")
    public CommonResult<CommonPage<VGoods>> getScreenVGoods(RebGoodsTwoDto dto){
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<VGoods> screenVGoods = vGoodsService.findScreenVGoods(dto);
        return CommonResult.success(CommonPage.restPage(screenVGoods));
    }

    @ApiOperation(value = "商品提交修改")
    @PostMapping("/getGoodsUpdate")
    public CommonResult<VGoods> getGoodsUpdate(Long ids,String vGoods){
        List<VGoods> list = JSON.parseArray(vGoods, VGoods.class);
        int vUpdate = vGoodsService.findVUpdate(ids, list);
        return vUpdate==1?CommonResult.success():CommonResult.failed();

    }

}

